package com.xb.loan.admin.service.dict.impl;
import com.xb.loan.admin.bean.dict.District;
import com.xb.loan.admin.dao.dict.DistrictDao;
import com.xb.loan.admin.service.dict.DistrictService;
import com.xb.loan.log.exception.BizException;
import com.xb.loan.util.cache.DefaultCacheFactory;
import com.xb.loan.util.common.ObjectOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * 地区码常量实现方法
 */
@Service("districtService")
public class DistrictServiceImpl implements DistrictService {
    @Autowired
    DistrictDao _districtDao;

    @Override
    public int deleteByDistrictId(String districtId) {
        return deleteByDistrictId(districtId, true);
    }

    @Override
    public int deleteByDistrictId(String districtId, boolean isEnableCache) {
        int rowCount = _districtDao.deleteByPrimaryKey(districtId);

        if (rowCount > 0 && isEnableCache) {

            DefaultCacheFactory.getFacotry().getCache().removeBean(District.class, districtId);
        }
        return rowCount;
    }

    @Override
    public int insert(District record) {
        return insert(record, true);
    }

    @Override
    public int insert(District record, boolean isEnableCache) {
        District district = ObjectOperation.convertByClasses(record, District.class);
        int rowCount = _districtDao.insert(district);

        if (rowCount > 0 && isEnableCache) {

            DefaultCacheFactory.getFacotry().getCache().setBean(District.class, record.getDistrictId(), record);
        }
        return rowCount;
    }

    @Override
    public District selectByDistrictId(String districtId) {
        return selectByDistrictId(districtId, true);
    }

    @Override
    public District selectByDistrictId(String districtId, boolean isEnableCache) {
        District _district = null;

        if (isEnableCache) {

            _district = DefaultCacheFactory.getFacotry().getCache().getBean(District.class, districtId);
        }

        if (_district == null) {

            _district = ObjectOperation.convertByClasses(_districtDao.selectByPrimaryKey(districtId), District.class);

            if (isEnableCache && _district != null) {

                DefaultCacheFactory.getFacotry().getCache().setBean(District.class, districtId, _district);
            }
        }
        return _district;
    }

    @Override
    public List<District> selectAll() {
        return selectAll(true);
    }

    @Override
    public List<District> selectAll(boolean isEnableCache) {
        List<District> _districtList = null;

        if (isEnableCache) {
            _districtList = DefaultCacheFactory.getFacotry().getCache().getBeans(District.class);
        }
        if (_districtList == null || _districtList.isEmpty()) {

            List<District> districtList = _districtDao.selectAll();

            _districtList = ObjectOperation.convertByClassesList(districtList, District.class);

            if (_districtList != null && isEnableCache) {

                for (District _district : _districtList) {

                    DefaultCacheFactory.getFacotry().getCache().setBean(District.class, _district.getDistrictId(), _district);
                }
            }
        }
        return _districtList;
    }

    @Override
    public int updateByDistrictId(District record) {
        return updateByDistrictId(record, true);
    }

    @Override
    public int updateByDistrictId(District record, boolean isEnableCache) {
        District district = ObjectOperation.convertByClasses(record, District.class);

        int rowCount = _districtDao.updateByPrimaryKey(district);

        if (rowCount > 0 && isEnableCache) {
            DefaultCacheFactory.getFacotry().getCache().setBean(District.class, record.getDistrictId(), record);
        }
        return rowCount;
    }

    /**
     * 根据地区上级编号查询地区列表
     * @param parentDistrictId 上级地区编号
     **/
    @Override
    public List<District> queryDistrictByParentDistrictId(String parentDistrictId) {
        return queryDistrictByParentDistrictId(parentDistrictId,true);
    }
    
    public List<District> queryDistrictByParentDistrictId(String parentDistrictId, boolean isEnableCache) {
        List<District> districtList=_districtDao.queryDistrictByParentDistrictId(parentDistrictId);
        
        return districtList;
        /*List<District> resultVO = new ArrayList<District>();
        List<District> tempVO = null;

        if (isEnableCache) {
            tempVO = DefaultCacheFactory.getFacotry().getCache().getBeans(District.class);
        }
        
        if (tempVO == null || tempVO.isEmpty()) {

            List<District> temp = _districtDao.selectAll();

            tempVO = ObjectOperation.convertByClassesList(temp, District.class);

            if (tempVO != null && isEnableCache) {

                for (District _district : tempVO) {

                    DefaultCacheFactory.getFacotry().getCache().setBean(District.class, _district.getDistrictId(), _district);
                }
            }
        }
        
        
        for(District _district:tempVO){
            if(parentDistrictId.equals(_district.getParentDistrictId())){
                resultVO.add(_district);
            }
        }
        
        return resultVO;*/
 
    }

	@Override
	public List<District> queryAllDistrictInfo(String areaId, int pageNum,
                                                 int numPerPage) throws BizException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("areaId", areaId);
		map.put("pageNum", pageNum);
		map.put("numPerPage", numPerPage);
		return ObjectOperation.convertByClassesList(_districtDao.queryAllDistrictInfo(map),District.class);
	}

	@Override
	public int queryAllDistrictInfoCount(String areaId)
			throws BizException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("areaId", areaId);
		return _districtDao.queryAllDistrictInfoCount(map);
	}

	@Override
	public List<District> queryDistrictByLikeDistrictId(String districtId) {
		List<District> list = _districtDao.queryDistrictByLikeDistrictId(districtId);
		return ObjectOperation.convertByClassesList(list,District.class);
	}
    
    
}